package com.qf.service.impl;

import com.qf.bean.Item;
import com.qf.dao.ItemDao;
import com.qf.service.ItemService;
import com.qf.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

/**
 * @author yao
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ItemDao itemDao;

    @Override
    public PageInfo<Item> findItemByCondition(String name, Integer page, Integer size) {
        //1. 查询数据总条数
        Integer count = itemDao.findCountByNameLike(name);
        //2. 创建分页工具类PageInfo.
        PageInfo<Item> pageInfo = new PageInfo(page,size,count);
        //3. 查询具体的list
        List<Item> list = itemDao.findByNameLikeLimit(name, pageInfo.getOffset(), pageInfo.getSize());
        //4. 将查询到的list封装到pageInfo
        pageInfo.setList(list);
        //5. 返回pageInfo.


        TreeSet<Integer> set = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return (int) o1 - (int) o2;
            }
        });


        return pageInfo;
    }

    @Override
    public Integer save(Item item) {
        return itemDao.save(item);
    }

    @Override
    public Integer deleteById(Integer id) {
        return itemDao.deleteById(id);
    }

    @Override
    public Item findById(Integer id) {
        return itemDao.findById(id);
    }

    @Override
    public Integer updateById(Item item) {
        return itemDao.updateById(item);
    }
}
